<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>安伴后台管理系统-添加设备海报</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/xadmin.css">
    <link rel="stylesheet" href="/static/css/webuploader.css">
    <script type="text/javascript" src="/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        .webuploader-pick {
            background: #009688 !important;
            height: 10px !important;
            line-height: 10px !important;
        }
    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form" id="data-form">
            {{.xsrfdata}}
            <input type="hidden" name="device_id" value="{{.deviceId}}">
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>海报标题
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="title" name="title" autocomplete="off" class="layui-input" lay-verify="">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>
            <div class="layui-form-item" style="margin-bottom: 0;" id="pic-form-item">
                <input type="hidden" id="url" name="url" value="">
                <label class="layui-form-label">海报图片</label>
                <div class="layui-input-inline">
                    <div id="picker">选择图片</div>
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>
            <div style="width: 200px;padding-left: 105px;margin-bottom: 15px;">
                <div id="progress-bar" class="layui-progress layui-progress-big layui-hide" lay-filter="picFile" lay-showPercent="yes">
                    <div class="layui-progress-bar layui-bg-green" lay-percent="0%"></div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <button class="layui-btn" lay-filter="do-submit" lay-submit="">添加</button>
            </div>
        </form>
    </div>
</div>

<script type="text/javascript" src="/static/js/jquery.min.js"></script>
<script type="text/javascript" src="/static/js/webuploader.min.js"></script>
<script>
    layui.use(['form', 'layer', 'element'],
        function () {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer,
                canSubmit = true,
                element = layui.element;

            var uploader = WebUploader.create({
                auto: true,
                swf: '/static/js/Uploader.swf',
                server: '{{urlfor "admin.UploadController.DevicePicture"}}',
                pick: {id: "#picker", multiple: false},
                resize: false,
                fileVal: "devicePicFile"
            });

            uploader.on( 'fileQueued', function(file) {
                $("#progress-bar").removeClass("layui-hide");
            });

            uploader.on( 'uploadProgress', function(file, percentage) {
                element.progress('picFile', percentage * 100 + '%');
            });

            uploader.on( 'uploadSuccess', function(file, res) {
                if (res.status == 1) {
                    $('#url').val(res.data);
                    setTimeout(function () {
                        $("#progress-bar").addClass("layui-hide");
                    }, 1500);
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            });

            //监听提交
            form.on('submit(do-submit)',
                function (data) {

                    if ($("#title").val().length <= 0) {
                        layer.msg("请输入海报标题", {icon: 2});
                        return false;
                    }
                    if ($("#url").val().length <= 0) {
                        layer.msg("请上传海报图片", {icon: 2});
                        return false;
                    }

                    if (!canSubmit) return false;
                    var loadIndex = layer.load();
                    canSubmit = false;

                    $.ajax({
                        url: "{{urlfor "admin.DevicePictureController.DoAdd"}}",
                        type: "POST",
                        data: $("#data-form").serialize(),
                        dataType: "json",
                        success: function (res) {

                            layer.close(loadIndex);
                            canSubmit = true;

                            if (res.status != 1) {
                                layer.msg(res.msg, {icon: 2});
                                return false;
                            }

                            layer.alert(res.msg, {icon: 6}, function () {
                                //关闭当前frame
                                xadmin.close();
                                // 可以对父窗口进行刷新
                                xadmin.father_reload();
                            });

                        }
                    });

                    return false;

                });

        });
</script>
</body>

</html>
